package com.hnevc.exam4zhouminmin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShowActivity extends AppCompatActivity {
    List<Food> foodList;
    //定义控件变量mListView
    ListView mListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //获取控件
        mListView = (ListView) findViewById(R.id.id_lv_foods);

        //访问网络，获取数据，构造FoodList
        new Thread(new Runnable() {
            @Override
            public void run() {
                //定义client对象
                OkHttpClient client = new OkHttpClient();
                //定义url,表示要访问的地址
                String url = "http://10.1.63.232:8080/RestaurantServer/Food/getFoods";
                //定义请求对象
                Request request = new Request.Builder()
                        .url(url)//表示请求的地址
                        .get()    //表示请求的访问为get
                        .build();  //构建request

                try {
                    Response response = client.newCall(request).execute();
                    String data = response.body().string();
                    //构建Gson解析对象
                    Gson gson = new Gson();
                    foodList = gson.fromJson(data,new TypeToken<List<Food>>(){}.getType());
                    //启用UI线程，更新界面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //创建适配器
                            MyBaseAdapter adapter = new MyBaseAdapter();
                            //使用适配器填充ListView
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();


    }
    //定义适配器类型
    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    }
}
